# vim:ts=4:sw=4:noet:
""" Semantic Checker Environments """

class Environment:
	GLOBAL, IVAR, METH_TEMP, BLOCK_ARG = range(4)
	typenames = [
		"global", 
		"instance variable",
		"method temporary",
		"block argument"
	]

	def __init__(self, checker, vars, parent, type, name, varBase=0):
		self.checker = checker
		self.vars = {}
		self.parent = parent
		if self.parent:
			for i,v in enumerate(vars):
				pe = self.has(v)
				if pe:
					self.checker.status.warning(
						"%s '%s' in '%s' shadows %s in '%s'" % (
						self.typenames[type], v, name, self.typenames[pe.type], pe.name))
				self.vars[v] = i
		else:
			for i,v in enumerate(vars):
				self.vars[v] = i
		self.type = type
		self.name = name
		self.varBase = varBase

	def has(self, v):
		if v in self.vars:
			return self
		if self.parent:
			pe = self.parent.has(v)
			if pe:
				return pe
		return None

	def get(self, v):
		if v in self.vars:
			return self.vars[v]
		if self.parent:
			pe = self.parent.get(v)
			if pe!=None:
				return pe
		return None

